package com.wakala.employment.fragment.other;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.android.tu.loadingdialog.LoadingDailog;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.CenterPopupView;
import com.lxj.xpopup.interfaces.OnCancelListener;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.wakala.employment.R;
import com.wakala.employment.activity.MainActivity;
import com.wakala.employment.adapter.entity.User;
import com.wakala.employment.core.BaseFragment;
import com.wakala.employment.core.http.subscriber.NoTipRequestSubscriber;
import com.wakala.employment.databinding.FragmentFeedBackBinding;
import com.wakala.employment.http.TestApi;
import com.wakala.employment.http.bean.FeedbackRequest;
import com.wakala.employment.utils.TokenUtils;
import com.xuexiang.xhttp2.XHttpProxy;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xutil.app.ActivityUtils;
import com.xuexiang.xutil.tip.ToastUtils;

import java.util.Objects;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FeedBackFragment#} factory method to
 * create an instance of this fragment.
 */
@Page(anim = CoreAnim.none)
public class FeedBackFragment extends BaseFragment<FragmentFeedBackBinding> implements View.OnClickListener {

    private static String TAG = FragmentFeedBackBinding.class.getSimpleName();

    private Handler mHandler = new Handler(Looper.getMainLooper());


    @NonNull
    @Override
    protected FragmentFeedBackBinding viewBindingInflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, boolean attachToRoot) {
        return FragmentFeedBackBinding.inflate(inflater, container, attachToRoot);
    }

    @Override
    protected TitleBar initTitle() {
        return super.initTitle().setTitle("意见反馈");
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void initListeners() {
        binding.stOnlineService.setOnClickListener(this);
        binding.stPhoneService.setOnClickListener(this);
        binding.btnFeedback.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.st_online_service) {
            new XPopup.Builder(getContext()).asCustom(new CenterPopupView(getContext()) {
                @Override
                protected int getImplLayoutId() {
                    return R.layout.dialogx_show_service;
                }

                @Override
                protected void onCreate() {
                    super.onCreate();
                    findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dismiss();
                        }
                    });
                    TextView title = findViewById(R.id.txt_dialog_title);
                    title.setText("微信搜索：河狸科技 公众号");
                }
            }).show();
        } else if (id == R.id.st_phone_service) {
            new XPopup.Builder(getContext()).asCustom(new CenterPopupView(getContext()) {
                @Override
                protected int getImplLayoutId() {
                    return R.layout.dialogx_show_service;
                }

                @Override
                protected void onCreate() {
                    super.onCreate();
                    findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dismiss();
                        }
                    });
                    TextView title = findViewById(R.id.txt_dialog_title);
                    title.setText("客服电话：" + getString(R.string.about_link_phone));
                    TextView serviceTip = findViewById(R.id.txt_service_tip);
                    serviceTip.setVisibility(View.VISIBLE);
                    serviceTip.setText("周一到周六 每天9：00-18：00");
                }
            }).show();
        } else if (id == R.id.btn_feedback) {
            {
                //提交
                new XPopup.Builder(getContext()).asConfirm("提示", "是否提交反馈信息？", "取消", "确定", new OnConfirmListener() {
                    @Override
                    public void onConfirm() {
                        //确定
                        if(binding.etContent.getContentText().length()>20){
                            postFeedBack();
                        }else{
                            ToastUtils.toast("反馈内容不能为空,且大于20个汉字");
                        }

                    }
                }, new OnCancelListener() {
                    @Override
                    public void onCancel() {
                        //取消
                    }
                }, false).show();
            }

        }
    }

    /**
     * 根据token获取当前登录用户信息
     */
    @SuppressLint("CheckResult")
    private void postFeedBack() {
        LoadingDailog.Builder loadBuilder = new LoadingDailog.Builder(getContext())
                .setMessage("提交数据中...")
                .setCancelable(true)
                .setCancelOutside(true);
        LoadingDailog dialog = loadBuilder.create();
        dialog.show();
        XHttpProxy.proxy(TestApi.IUniversal.class)
                .postFeedback(new FeedbackRequest(Objects.requireNonNull(binding.etPhone.getText()).toString(), binding.etContent.getContentText()))
                .subscribeWith(new NoTipRequestSubscriber<Void>() {
                    @SuppressLint("CheckResult")
                    @Override
                    public void onSuccess(Void v) {
                        dialog.dismiss();
                        ToastUtils.toast("感谢您提交反馈意见，客服会及时跟进处理");
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                popToBack();
                            }
                        }, 2000);

                    }

                    @Override
                    public void onError(ApiException e) {
                        dialog.dismiss();
                        super.onError(e);
                        ToastUtils.toast(e.getDisplayMessage());
                    }
                });
    }
}